package web;

import com.google.gson.Gson;
import com.mysql.cj.Session;
import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

public class WelcomeServlet extends BaseServlet{

    private UserService service = new UserServiceImpl();

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        String uid = req.getParameter("uid");
        String password = req.getParameter("password");

        System.out.println(uid+"\t"+password);

        User loginUser = service.login(new User(uid,null,password));
        if (loginUser == null){
            resp.getWriter().write("false");
        }else {
            Gson gson = new Gson();
            resp.getWriter().write(gson.toJson(loginUser));
        }
    }

    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        String uid = req.getParameter("uid");
        String nikename= req.getParameter("nikename");
        String password = req.getParameter("password");

        System.out.println(uid+"\t"+nikename+"\t"+password);

        int register = service.register(new User(uid,nikename,password));

        if (register != 1){
            resp.getWriter().write("false");
        }else {
            resp.getWriter().write("true");
        }
    }



}
